/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.sql.ResultSet;
import dal.StaticticsDAO;
import dto.BaiThiDTO;
import java.io.*;
import java.util.ArrayList;

/**
 *
 * @author Stanley
 */
public class ModuleStaticticsController {

    private StaticticsDAO dao = null;

    public ModuleStaticticsController() {
        this.dao = new StaticticsDAO();
    }

    public ResultSet getBaiThi(String userType, String maTS, int start, int pageSize) {
        return dao.getBaiThi(userType, maTS, start, pageSize);
    }

    public int countBaiThi(String userType, String maTS) {
        return dao.countBaiThi(userType, maTS);
    }

    public String getBaiThiToDrawChart(String jsonFile, String maTS) {
        ArrayList dsBaiThiDe = dao.getBaiThi(maTS, "1");
        ArrayList dsBaiThiTB = dao.getBaiThi(maTS, "2");
        ArrayList dsBaiThiKho = dao.getBaiThi(maTS, "3");

        // write json file
        //jsonFile = "d:/data.json";
        try {
            FileWriter outFile = new FileWriter(jsonFile);
            PrintWriter out = new PrintWriter(outFile);

            /*
             * GHI HEADER
             */
            out.println("{");
            out.println("\"JSChart\" : {");
            out.println("\"datasets\" : [");

            /*
             * GHI BAI THI DE
             */
            int n = 0;
            BaiThiDTO info = null;
            if (dsBaiThiDe != null) {
                out.println("{");
                out.println("\"type\" : \"line\",");
                out.println("\"id\" : \"blue\",");
                out.println("\"data\" : [");

                n = dsBaiThiDe.size();
                for (int i = 0; i < n - 1; i++) {
                    info = (BaiThiDTO) dsBaiThiDe.get(i);
                    out.println("{");
                    out.println("\"unit\" : \"" + Integer.toString(i + 1) + "\",");
                    out.println("\"value\" : " + info.getDiem());
                    out.println("},");
                }
                // ghi bai thi n - 1
                info = (BaiThiDTO) dsBaiThiDe.get(n - 1);
                out.println("{");
                out.println("\"unit\" : \"" + Integer.toString(n) + "\",");
                out.println("\"value\" : " + info.getDiem());
                out.println("}");
                if (dsBaiThiKho == null && dsBaiThiTB == null) {
                    out.println("]\n}");
                } else {
                    out.println("]\n},");
                }
            }


            /*
             * GHI BAI THI TRUNG BINH
             */
            if (dsBaiThiTB != null) {
                out.println("{");
                out.println("\"type\" : \"line\",");
                out.println("\"id\" : \"green\",");
                out.println("\"data\" : [");

                n = dsBaiThiTB.size();
                for (int i = 0; i < n - 1; i++) {
                    info = (BaiThiDTO) dsBaiThiTB.get(i);
                    out.println("{");
                    out.println("\"unit\" : \"" + Integer.toString(i + 1) + "\",");
                    out.println("\"value\" : " + info.getDiem());
                    out.println("},");
                }
                // ghi bai thi n - 1
                info = (BaiThiDTO) dsBaiThiTB.get(n - 1);
                out.println("{");
                out.println("\"unit\" : \"" + Integer.toString(n) + "\",");
                out.println("\"value\" : " + info.getDiem());
                out.println("}");

                if (dsBaiThiKho == null) {
                    out.println("]\n}");
                } else {
                    out.println("]\n},");
                }
            }


            /*
             * GHI BAI THI KHO
             */
            if (dsBaiThiKho != null) {
                out.println("{");
                out.println("\"type\" : \"line\",");
                out.println("\"id\" : \"red\",");
                out.println("\"data\" : [");

                n = dsBaiThiKho.size();
                for (int i = 0; i < n - 1; i++) {
                    info = (BaiThiDTO) dsBaiThiKho.get(i);
                    out.println("{");
                    out.println("\"unit\" : \"" + Integer.toString(i + 1) + "\",");
                    out.println("\"value\" : " + info.getDiem());
                    out.println("},");
                }
                // ghi bai thi n - 1
                info = (BaiThiDTO) dsBaiThiKho.get(n - 1);
                out.println("{");
                out.println("\"unit\" : \"" + Integer.toString(n) + "\",");
                out.println("\"value\" : " + info.getDiem());
                out.println("}");

                out.println("]\n}");
            }
            out.println("],");

            /*
             * GHI OPTION
             */
            out.println("\"optionset\" : [");
            out.println("{");
            out.println("\"set\" : \"setSize\",");
            out.println("\"value\" : \"930, 300\"");
            out.println("},");

            out.println("{");
            out.println("\"set\" : \"setAxisValuesNumberY\",");
            out.println("\"value\" : \"6\"");
            out.println("},");

            out.println("{");
            out.println("\"set\" : \"setIntervalStartY\",");
            out.println("\"value\" : \"-2.5\"");
            out.println("},");

            out.println("{");
            out.println("\"set\" : \"setIntervalEndY\",");
            out.println("\"value\" : \"10\"");
            out.println("},");

            out.println("{");
            out.println("\"set\" : \"setLabelX\",");
            out.println("\"value\" : \"[1,'Lan 1']\"");
            out.println("},");

            out.println("{");
            out.println("\"set\" : \"setLabelX\",");
            out.println("\"value\" : \"[2,'Lan 2']\"");
            out.println("},");

            out.println("{");
            out.println("\"set\" : \"setLabelX\",");
            out.println("\"value\" : \"[3,'Lan 3']\"");
            out.println("},");

            out.println("{");
            out.println("\"set\" : \"setLabelX\",");
            out.println("\"value\" : \"[4,'Lan 4']\"");
            out.println("},");

            out.println("{");
            out.println("\"set\" : \"setLabelX\",");
            out.println("\"value\" : \"[5,'Lan 5']\"");
            out.println("},");

            out.println("{");
            out.println("\"set\" : \"setLabelX\",");
            out.println("\"value\" : \"[6,'Lan 6']\"");
            out.println("},");

            out.println("{");
            out.println("\"set\" : \"setLabelX\",");
            out.println("\"value\" : \"[7,'Lan 7']\"");
            out.println("},");

            out.println("{");
            out.println("\"set\" : \"setLabelX\",");
            out.println("\"value\" : \"[8,'Lan 8']\"");
            out.println("},");

            out.println("{");
            out.println("\"set\" : \"setLabelX\",");
            out.println("\"value\" : \"[9,'Lan 9']\"");
            out.println("},");

            out.println("{");
            out.println("\"set\" : \"setLabelX\",");
            out.println("\"value\" : \"[10,'Lan 10']\"");
            out.println("},");

            out.println("{");
            out.println("\"set\" : \"setAxisValuesNumberX\",");
            out.println("\"value\" : \"10\"");
            out.println("},");

            out.println("{");
            out.println("\"set\" : \"setShowXValues\",");
            out.println("\"value\" : \"false\"");
            out.println("},");

            out.println("{");
            out.println("\"set\" : \"setTitleColor\",");
            out.println("\"value\" : \"'#454545'\"");
            out.println("},");

            out.println("{");
            out.println("\"set\" : \"setAxisValuesColor\",");
            out.println("\"value\" : \"'#454545'\"");
            out.println("},");

            out.println("{");
            out.println("\"set\" : \"setLineColor\",");
            out.println("\"value\" : \"'#A4D314', 'green'\"");
            out.println("},");

            out.println("{");
            out.println("\"set\" : \"setLineColor\",");
            out.println("\"value\" : \"'#FF0000', 'red'\"");
            out.println("},");

            out.println("{");
            out.println("\"set\" : \"setLegendShow\",");
            out.println("\"value\" : \"true\"");
            out.println("},");

            out.println("{");
            out.println("\"set\" : \"setLegendPosition\",");
            out.println("\"value\" : \"820, 80\"");
            out.println("},");

            if (dsBaiThiDe != null) {

                out.println("{");
                out.println("\"set\" : \"setLegendForLine\",");
                out.println("\"value\" : \"'blue', 'De'\"");
                if (dsBaiThiKho != null || dsBaiThiTB != null) {

                    out.println("},");
                } else {
                    out.println("}");
                }
            }

            if (dsBaiThiTB != null) {
                out.println("{");
                out.println("\"set\" : \"setLegendForLine\",");
                out.println("\"value\" : \"'green', 'Trung binh'\"");
                if (dsBaiThiKho != null) {
                    out.println("},");
                } else {
                    out.println("}");
                }
            }
            if (dsBaiThiKho != null) {
                out.println("{");
                out.println("\"set\" : \"setLegendForLine\",");
                out.println("\"value\" : \"'red', 'Kho'\"");
                out.println("}");
            }

            /*
             * GHI FOOTER
             */
            out.println("]");
            out.println("}");
            out.println("}");

            out.close();
        } catch (Exception e) {
            System.out.println("Error occur when write to file");
        }
        return jsonFile;
    }
}
